package com.example.petshopping.ui.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatImageView;

public class CircularImageView extends AppCompatImageView {
    private Paint paint;
    private Paint borderPaint;
    private int borderWidth = 4; // 边框宽度，单位为像素

    public CircularImageView(Context context) {
        super(context);
        init();
    }

    public CircularImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CircularImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);

        borderPaint = new Paint();
        borderPaint.setAntiAlias(true);
        borderPaint.setColor(0xFFFFFFFF); // 白色边框
        borderPaint.setStyle(Paint.Style.STROKE);
        borderPaint.setStrokeWidth(borderWidth);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Drawable drawable = getDrawable();
        if (drawable == null) {
            return;
        }

        if (getWidth() == 0 || getHeight() == 0) {
            return;
        }

        Bitmap bitmap = getBitmapFromDrawable(drawable);
        if (bitmap == null) {
            return;
        }

        BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);

        // 根据控件大小缩放图片
        float scale;
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        float radius = Math.min(getWidth(), getHeight()) / 2f;

        if (width > height) {
            scale = getHeight() / (float) height;
        } else {
            scale = getWidth() / (float) width;
        }

        shader.setLocalMatrix(getImageMatrix());
        paint.setShader(shader);

        // 绘制圆形图像
        canvas.drawCircle(getWidth() / 2f, getHeight() / 2f, radius - borderWidth / 2f, paint);

        // 绘制白色边框
        canvas.drawCircle(getWidth() / 2f, getHeight() / 2f, radius - borderWidth / 2f, borderPaint);
    }

    private Bitmap getBitmapFromDrawable(Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        }

        try {
            Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            drawable.draw(canvas);
            return bitmap;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public void setBorderWidth(int borderWidthInPixels) {
        this.borderWidth = borderWidthInPixels;
        borderPaint.setStrokeWidth(borderWidth);
        invalidate();
    }

    public void setBorderColor(int borderColor) {
        borderPaint.setColor(borderColor);
        invalidate();
    }
} 